const connection = require("../app/database");
const dayjs = require("dayjs");
const { v4: uuidv4 } = require("uuid");

class TagService {
  constructor() {
    this.formName = "blogs";
  }
  // 新增
  async create(_params) {
    return new Promise(async (resolve, reject) => {
      try {
        const { title, content, userId, tagId, imageList, hotNum, commentNum, description } = _params;
        const id = uuidv4();
        const createTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
        const statement = `INSERT INTO ${this.formName} (id, title, content, user_id, tag_id, createTime, updateTime, imageList, hotNum, commentNum, description ) VALUES (?,?,?,?,?,?,?,?,?,?,?);`;
        const res = await connection.execute(statement, [
          id,
          title,
          content,
          userId,
          tagId,
          createTime,
          createTime,
          imageList,
          hotNum,
          commentNum,
          description,
        ]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 批量删除
  async delete(_ids) {
    return new Promise(async (resolve, reject) => {
      try {
        const placeholders = _ids.map(() => "?").join(",");
        const statement = `DELETE FROM ${this.formName} WHERE id IN (${placeholders});`;
        const [res] = await connection.execute(statement, _ids);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 分页
  async page(_params) {
    return new Promise(async (resolve, reject) => {
      const { pageNum, pageSize, userId, title = "", tagId = "" } = _params;

      const offset = (pageNum - 1) * pageSize;
      const limit = pageSize;
      try {
        const statement = `
           SELECT * FROM ${this.formName}
           WHERE title LIKE ? AND user_id = ? And tag_id LIKE ?
           ORDER BY updateTime DESC
           LIMIT ${offset}, ${limit};
         `;
        const [res] = await connection.execute(statement, [`%${title}%`, userId, `%${tagId}%`]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 编辑
  async update(_params) {
    const { id, content, title, tagId = "", imageList = "", hotNum = 0, commentNum = 0, description = "" } = _params;
    const updateTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `UPDATE ${this.formName} SET content = ?, title = ?, tag_id = ?, updateTime = ?, imageList = ?, hotNum = ?, commentNum = ?, description = ?  WHERE id = ?;`;
        const res = await connection.execute(statement, [
          content,
          title,
          tagId,
          updateTime,
          imageList,
          hotNum,
          commentNum,
          description,
          id,
        ]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 查询
  async findName(_params) {
    const { title, userId } = _params;
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `SELECT * FROM ${this.formName} WHERE title = ? && user_id = ?;`;
        const [res] = await connection.execute(statement, [title, userId]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }

  // 总数
  async allNum(_userId, title = "", tagId = "") {
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `
        SELECT COUNT(*) 
        FROM ${this.formName} 
        WHERE user_id = ? AND title LIKE ? AND tag_id LIKE ?;
        `;
        const [res] = await connection.execute(statement, [_userId, `%${title}%`, `%${tagId}%`]);
        resolve(res[0]["COUNT(*)"]);
      } catch (error) {
        reject(error);
      }
    });
  }

  // 详情
  async detail(_id) {
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `SELECT * FROM ${this.formName} WHERE id = ?;`;
        const [res] = await connection.execute(statement, [_id]);
        resolve(res[0]);
      } catch (error) {
        reject(error);
      }
    });
  }

  // 热度+1
  async updateHot(_id) {
    const statement = `SELECT * FROM ${this.formName} WHERE id = ?;`;
    const [detailRes] = await connection.execute(statement, [_id]);
    const { hotNum } = detailRes[0];

    return new Promise(async (resolve, reject) => {
      try {
        const statement = `UPDATE ${this.formName} SET hotNum = ? WHERE id = ?;`;
        const res = await connection.execute(statement, [Number(hotNum) + 1, _id]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
}

module.exports = new TagService();
